package com.mall.cart.server.controller;

import com.mall.cart.common.domain.Cart;
import com.mall.cart.common.domain.CartItem;
import com.mall.common.core.domain.Result;
import com.mall.cart.server.service.CartService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     * @param cartItem
     * @return
     */
    @PostMapping("/addCart")
    public Result addCart(@RequestBody CartItem cartItem){
        return cartService.addCart(cartItem);
    }

    /**
     * 基于redis实现添加购物车
    */
    @PostMapping("/add/{skuId}")
    public Result addCart(@PathVariable Long skuId){
        return cartService.add(skuId);
    }

    /**
     * 查询购物车列表
     */
    @GetMapping("/findCartList")
    public Result<List<Cart>> findCartList(){
        return cartService.findCartList();
    }


    /**
     * 支付:根据skuId字符串删除缓存以及数据库数据
     * @param skuIdListByJson
     */
    public void clearCartByPay(String skuIdListByJson){
        cartService.clearCartByPay(skuIdListByJson);
    }


    /**
     * 根据skuId 查询redis并修改
     * @param skuId
     * @param skuCount
     */
    @GetMapping("/updateSaleCount/{skuId}/{skuCount}")
    public Result updateSaleCount(@PathVariable Integer skuId,@PathVariable Integer skuCount){
        return cartService.updateSaleCount(skuId,skuCount);
    }

}
